﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Delivery
{
    /// <summary>
    /// DeliveryTemplateUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class DeliveryTemplateUserControl : UserControl, IPlugin
    {
        private DeliveryTemplateService deliveryTemplateService = ServiceContainer.GetService<DeliveryTemplateService>();
        private List<DeliveryTemplateVM> templates = null;
        public DeliveryTemplateUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return "运费模板"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var window = new DeliveryTemplateEditWindow();
                bool? ret = window.ShowDialog();
                Mengsk.ProxyDelivery.Domain.DeliveryTemplate template = window.DeliveryTemplate;
                if (template.Id > 0)
                {
                    var list = this.lstDeliveryTemplates.ItemsSource.OfType<DeliveryTemplateVM>().ToList();
                    list.Add(new DeliveryTemplateVM { Source = window.DeliveryTemplate });
                    this.lstDeliveryTemplates.ItemsSource = null;
                    this.templates = list.OrderBy(obj => obj.Source).ToList();
                    this.lstDeliveryTemplates.ItemsSource = this.templates;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button btn = sender as Button;
                DeliveryTemplateVM vm = btn.DataContext as DeliveryTemplateVM;

                if (MessageBox.Show("是否删除:" + vm.Source.Name, "警告", MessageBoxButton.YesNo, MessageBoxImage.Warning) != MessageBoxResult.Yes)
                {
                    return;
                }

                if (MessageBox.Show("是否删除:" + vm.Source.Name, "警告", MessageBoxButton.YesNo, MessageBoxImage.Warning) != MessageBoxResult.Yes)
                {
                    return;
                }
                this.deliveryTemplateService.Delete(vm.Source);
                var list = this.lstDeliveryTemplates.ItemsSource.OfType<DeliveryTemplateVM>().ToList();
                list.RemoveAll(obj => obj.Source.Id == vm.Source.Id);
                this.lstDeliveryTemplates.ItemsSource = null;
                this.lstDeliveryTemplates.ItemsSource = list;
                MessageBox.Show("删除成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button btn = sender as Button;
                DeliveryTemplateVM vm = btn.DataContext as DeliveryTemplateVM;
                var window = new DeliveryTemplateEditWindow { DeliveryTemplate = vm.Source };
                bool? ret = window.ShowDialog();
                if (window.NeedUpdate)
                {
                    var list = this.lstDeliveryTemplates.ItemsSource;
                    this.lstDeliveryTemplates.ItemsSource = null;
                    this.lstDeliveryTemplates.ItemsSource = list;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDisabled_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button btn = sender as Button;
                DeliveryTemplateVM vm = btn.DataContext as DeliveryTemplateVM;
                vm.Source.Enabled = !vm.Source.Enabled;
                this.deliveryTemplateService.Update(vm.Source);
                var list = this.lstDeliveryTemplates.ItemsSource;
                this.lstDeliveryTemplates.ItemsSource = null;
                this.lstDeliveryTemplates.ItemsSource = list;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var templates = this.deliveryTemplateService.GetAllByField("warehouse", App.LoginOperator.Warehouse, 0, 0).Datas.ToList();
                templates.Sort();
                this.templates = templates.Select(obj => new DeliveryTemplateVM { Source = obj }).ToList();
                this.lstDeliveryTemplates.ItemsSource = this.templates;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDisabledWebShow_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button btn = sender as Button;
                DeliveryTemplateVM vm = btn.DataContext as DeliveryTemplateVM;
                vm.Source.WebShowEnabled = !vm.Source.WebShowEnabled;
                this.deliveryTemplateService.Update(vm.Source);
                var list = this.lstDeliveryTemplates.ItemsSource;
                this.lstDeliveryTemplates.ItemsSource = null;
                this.lstDeliveryTemplates.ItemsSource = list;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnUp_Click(object sender, RoutedEventArgs e)
        {
            this.Move(-1, sender);
        }

        private void btnDown_Click(object sender, RoutedEventArgs e)
        {
            this.Move(1, sender);
        }

        private void Move(int step, object sender)
        {
            try
            {
                if (this.templates == null || this.templates.Count < 2)
                {
                    return;
                }
                FrameworkElement fe = sender as FrameworkElement;
                if (fe == null)
                {
                    return;
                }
                var template = fe.DataContext as DeliveryTemplateVM;
                if (template == null)
                {
                    return;
                }

                int index = this.templates.IndexOf(template);
                if (index + step > this.templates.Count - 1 || index + step < 0)
                {
                    return;
                }
                List<DeliveryTemplateVM> tmps = new List<DeliveryTemplateVM>(this.templates);
                tmps.Remove(template);
                tmps.Insert(index + step, template);
                int[] orders = tmps.Select(obj => obj.Source.Id).ToArray();
                this.deliveryTemplateService.AdjustOrder(orders);
                this.templates = tmps;
                this.lstDeliveryTemplates.ItemsSource = tmps;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
